package com.shanshan;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.builder.CustomFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.apache.ibatis.type.JdbcType;
import org.springframework.util.StringUtils;

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hpl
 * @since 2024/5/23
 */
public class CodeGet {

    public static void generator(DataModel data) {
        FastAutoGenerator.create(data.getUrl(), data.getUser(), data.getPassword())
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            // 兼容旧版本转换成Integer
                            if (JdbcType.TINYINT == metaInfo.getJdbcType()) {
                                return DbColumnType.INTEGER;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        }))
                .globalConfig(builder -> builder
                        .author(data.getAuthor())
//                        .outputDir(Paths.get(System.getProperty("user.dir")) + "/demo")
                        .outputDir(Paths.get(System.getProperty("user.dir")).toString())
                        .commentDate("yyyy-MM-dd")
                        .disableOpenDir()
                )
                .packageConfig(builder -> builder
                        .parent(data.getPackageName())
                        .entity("business." + data.getPackageName() + ".model")
                        .mapper("business." + data.getPackageName() + ".mapper")
                        .xml("business." + data.getPackageName() + ".mapper.xml")
                        .controller("dzmf." + data.getPackageName() + ".controller")
                )
                .strategyConfig(builder -> builder
                        .addInclude(data.getTableName())
                        .addTablePrefix("dzmf_video_", "t_", "dzmf_")
                        .entityBuilder()
                        .enableLombok()
                        .mapperBuilder()
                        .mapperTemplate("/templates/custom/mapper.java")
                        .mapperXmlTemplate("/templates/custom/mapper.xml")
                        .entityBuilder()
                        .javaTemplate("/templates/custom/entity.java")
                        .controllerBuilder()
                        .template("/templates/custom/controller.java")
                )
                //6、自定义配置
                .injectionConfig(consumer -> {
                    Map<String, Object> customMap = new HashMap<>();
                    customMap.put("vo", data.getPackageName() + ".vo");
                    customMap.put("request", data.getPackageName() + ".request");
                    customMap.put("api", data.getPackageName() + ".api");
                    customMap.put("rpc", data.getPackageName() + ".rpc");
                    customMap.put("service", data.getPackageName() + ".service");
                    customMap.put("serviceImpl", data.getPackageName() + ".service.impl");
                    customMap.put("model", data.getPackageName() + ".model");
                    customMap.put("html", data.getPackageName() + ".html");
                    customMap.put("js", data.getPackageName() + ".js");
                    customMap.put("menu", data.getPackageName() + ".sql");
                    customMap.put("moduleName", data.getModuleName());
                    consumer.customMap(customMap);
                    // DTO
                    List<CustomFile> customFiles = new ArrayList<>();
                    customFiles.add(new CustomFile.Builder().packageName("api." + data.getPackageName() + ".vo").fileName("VO.java")
                            .templatePath("/templates/custom/vo.java.ftl").enableFileOverride().build());
                    customFiles.add(new CustomFile.Builder().packageName("api." + data.getPackageName() + ".request").fileName("Req.java")
                            .templatePath("/templates/custom/req.java.ftl").enableFileOverride().build());
                    customFiles.add(new CustomFile.Builder().packageName("api." + data.getPackageName() + ".service").fileName("ServiceRpc.java")
                            .templatePath("/templates/custom/api.java.ftl").enableFileOverride().build());
                    customFiles.add(new CustomFile.Builder().packageName("business." + data.getPackageName() + ".rpc").fileName("ServiceRpcImpl.java")
                            .templatePath("/templates/custom/rpc.java.ftl").enableFileOverride().build());
                    customFiles.add(new CustomFile.Builder().packageName("business." + data.getPackageName() + ".service").fileName("Service.java")
                            .templatePath("/templates/custom/service.java.ftl").enableFileOverride().build());
                    customFiles.add(new CustomFile.Builder().packageName("business." + data.getPackageName() + ".service" + ".impl").fileName("ServiceImpl.java")
                            .templatePath("/templates/custom/serviceImpl.java.ftl").enableFileOverride().build());
                    customFiles.add(new CustomFile.Builder().packageName("dzmf." + data.getPackageName() + ".model").fileName("Dict.java")
                            .templatePath("/templates/custom/dict.java.ftl").enableFileOverride().build());
                    customFiles.add(new CustomFile.Builder().packageName("view." + data.getPackageName()).fileName(".html").formatNameFunction(name -> StringUtils.uncapitalize(name.getEntityName()))
                            .templatePath("/templates/custom/index.html.ftl").enableFileOverride().build());
                    customFiles.add(new CustomFile.Builder().packageName("view." + data.getPackageName()).fileName("_add.html").formatNameFunction(name -> StringUtils.uncapitalize(name.getEntityName()))
                            .templatePath("/templates/custom/add.html.ftl").enableFileOverride().build());
                    customFiles.add(new CustomFile.Builder().packageName("view." + data.getPackageName()).fileName("_edit.html").formatNameFunction(name -> StringUtils.uncapitalize(name.getEntityName()))
                            .templatePath("/templates/custom/edit.html.ftl").enableFileOverride().build());
                    customFiles.add(new CustomFile.Builder().packageName("static." + data.getPackageName()).fileName(".js").formatNameFunction(name -> StringUtils.uncapitalize(name.getEntityName()))
                            .templatePath("/templates/custom/page.js.ftl").enableFileOverride().build());
                    customFiles.add(new CustomFile.Builder().packageName("static." + data.getPackageName()).fileName("_info.js").formatNameFunction(name -> StringUtils.uncapitalize(name.getEntityName()))
                            .templatePath("/templates/custom/page_info.js.ftl").enableFileOverride().build());
                    customFiles.add(new CustomFile.Builder().packageName("sql").fileName("_menu.sql")
                            .templatePath("/templates/custom/menu.sql.ftl").enableFileOverride().build());
                    consumer.customFile(customFiles);
                })
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}

